package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;

public class MessageAddUser {
	
	private ConnectionToClient client ;
	private ArrayList<Object> str ; 
	
	public int row1=0;
	
public MessageAddUser(ArrayList<Object> msg, ConnectionToClient client) {
		
		this.client= client ;
		this.str= msg; 		
		
	}

public void Run(String FirstName, String LastName, String Password, String Email) throws SQLException, IOException {
	
	SQLConnection sc = new SQLConnection();
	
	
	PreparedStatement AllBank = sc.getConnection().prepareStatement("SELECT MAX(Userid) FROM hts.users;");
	
	ResultSet Allrs= AllBank.executeQuery();
	row1 = 0;
	while(Allrs.next()){
		int temp = Allrs.getInt(1);
			if(row1>temp)
				row1=row1;
				else{
				row1=temp;
				}
	
	PreparedStatement Bankps = sc.getConnection().prepareStatement("INSERT INTO users (Userid,FirstName,LastName,Password,EMail,Role,Connected) VALUES(?,?,?,?,?,?,?);");
	Bankps.setInt(1, this.row1+1);
	Bankps.setString(2,FirstName);
	Bankps.setString(3,LastName);
	Bankps.setString(4,Password);
	Bankps.setString(5,Email);
	Bankps.setString(6,"0");
	Bankps.setString(7,"0");
	Bankps.executeUpdate();
	Bankps.close();
	
}   
	AllBank.close();
	
	ArrayList<Object> Bank =new ArrayList<Object>();
	Bank.add("AddUser");
	client.sendToClient(Bank);


}}






